package com.usercenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usercenter.entity.WorkerProfileExtendEntity;
import com.usercenter.mapper.WorkerProfileMapper;
import com.usercenter.service.WorkerProfileService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 维修工个人资料服务实现
 */
@Service
public class WorkerProfileServiceImpl extends ServiceImpl<WorkerProfileMapper, WorkerProfileExtendEntity> implements WorkerProfileService {

    @Override
    public WorkerProfileExtendEntity getProfileByOpenId(String openid) {
        LambdaQueryWrapper<WorkerProfileExtendEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WorkerProfileExtendEntity::getOpenid, openid);
        return this.getOne(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateProfile(WorkerProfileExtendEntity profile) {
        profile.setUpdateTime(new Date());
        return this.updateById(profile);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean initProfile(String openid) {
        // 检查是否已存在
        if (getProfileByOpenId(openid) != null) {
            return true;
        }
        
        // 创建新的个人资料
        WorkerProfileExtendEntity profile = new WorkerProfileExtendEntity();
        profile.setOpenid(openid);
        profile.setGender(0);  // 默认未知
        profile.setSignature("专业维修，用心服务");
        profile.setSkillLevel(1); // 默认初级
        profile.setWorkYears(0);
        profile.setUpdateTime(new Date());
        
        return this.save(profile);
    }
    
    @Override
    public Object getSkillLevelInfo() {
        List<Map<String, Object>> skillLevels = new ArrayList<>();
        
        Map<String, Object> level1 = new HashMap<>();
        level1.put("level", 1);
        level1.put("name", "初级维修工");
        level1.put("description", "能够处理基本维修任务，有基础维修知识");
        
        Map<String, Object> level2 = new HashMap<>();
        level2.put("level", 2);
        level2.put("name", "中级维修工");
        level2.put("description", "能够处理复杂维修任务，具备丰富的维修经验");
        
        Map<String, Object> level3 = new HashMap<>();
        level3.put("level", 3);
        level3.put("name", "高级维修工");
        level3.put("description", "能够处理各类疑难问题，并有指导培训其他维修工的能力");
        
        skillLevels.add(level1);
        skillLevels.add(level2);
        skillLevels.add(level3);
        
        return skillLevels;
    }
} 